import { defineStore } from "pinia";
import { getList } from "@/api/alarmRescue/alarmSetup";
import { setIcon } from "@/utils/setIcon"

const useMqttStore = defineStore('mqtt', {
  state: () => ({
    aircraftList: [],
    droneList: [],
    brandUAVList: [],
    homemadeUAVList: [],
    planeList: [],
    minhangList: [],
    isShowDrone: true,
    isShowBrandUAV: true,
    isShowHomemadeUAV: true,
    isShowPlane: true,
    isShowMHPlane: true,
    topHeight: null,
    lowHeight: null,
    searchList: [],
    alarmList: [],
    alarmType: {},
    tempList: []
  }),
  actions: {
    // 添加无人机飞行器
    addDrone(info) {
      let icon;
      let index;
      // console.log("info.droneType", info.droneType);
      const { reportStatus, intrusionStatus, droneUK, type, droneType } = info;
      // let index = this.droneList.findIndex(item => item.id === droneUK);
      // 根据是品牌无人机还是自制无人机查询不同的数组
      if (droneType === 1) index = this.brandUAVList.findIndex(item => item.id === droneUK);
      if (droneType === 2) index = this.homemadeUAVList.findIndex(item => item.id === droneUK);
      icon = setIcon(info);
      let data = {
        icon,
        EntityType: "aircraft",
        id: droneUK,
        longitude: info.curLongitude,
        latitude: info.curLatitude,
        height: info.curHeight,
        ...info
      }
      if (index > -1) {
        // 根据无人机类型添加到对应的数组中
        if (droneType === 1) this.brandUAVList.splice(index, 1, { ...data });
        if (droneType === 2) this.homemadeUAVList.splice(index, 1, { ...data });
        let index1 = this.aircraftList.findIndex(item => item.id === droneUK);
        this.aircraftList.splice(index1, 1, { ...data })
      } else {
        // 根据无人机类型添加到对应的数组中
        if (droneType === 1) this.brandUAVList.push({ ...data });
        if (droneType === 2) this.homemadeUAVList.push({ ...data });
        // this.droneList.push({ ...data })
        this.aircraftList.push({ ...data })
      }
      this.alarmEvents({ ...data });
      if (this.isShowBrandUAV && droneType === 1 || this.isShowHomemadeUAV && droneType === 2) return { data: { ...data } };
      return {}
    },

    // 移除无人机飞行器
    removeDrone(info) {
      const { droneUK, droneType } = info;
      let data = null;
      let index;
      // = this.droneList.findIndex(item => item.id === droneUK)
      if (droneType === 1) index = this.brandUAVList.findIndex(item => item.id === droneUK);
      if (droneType === 2) index = this.homemadeUAVList.findIndex(item => item.id === droneUK);
      if (index > -1) {
        if (droneType === 1) {
          data = { ...this.brandUAVList[index] };
          this.brandUAVList.splice(index, 1)
        }
        if (droneType === 2) {
          data = { ...this.homemadeUAVList[index] };
          this.homemadeUAVList.splice(index, 1)
        }
      }
      let index1 = this.aircraftList.findIndex(item => item.id === droneUK)
      if (index1 > -1) this.aircraftList.splice(index1, 1);
      this.alarmEvents({ ...data, landFlag: 2 });
      if (this.isShowBrandUAV && droneType === 1 || this.isShowHomemadeUAV && droneType === 2) return data;
      return
    },

    // 添加通航
    addPlane(info) {
      let icon;
      const { reportStatus, intrusionStatus, droneUK } = info;
      let index = this.planeList.findIndex(item => item.id === droneUK);
      if (reportStatus === 0) icon = "PlaneBlack.png";
      else icon = "PlaneNormal.png";
      let data = {
        icon,
        EntityType: "aircraft",
        id: droneUK,
        longitude: info.curLongitude,
        latitude: info.curLatitude,
        height: info.curHeight,
        ...info
      }
      if (index > -1) {
        this.planeList.splice(index, 1, { ...data })
        let index1 = this.aircraftList.findIndex(item => item.id === droneUK);
        this.aircraftList.splice(index1, 1, { ...data })
      } else {
        this.planeList.push({ ...data })
        this.aircraftList.push({ ...data })
      }

      this.alarmEvents({ ...data });
      if (this.isShowPlane) return { data: { ...data } };
      return {};
    },
    // 移除通航飞行器
    removePlane(info) {
      const { droneUK } = info;
      let data = null;
      let index = this.planeList.findIndex(item => item.id === droneUK);
      if (index > -1) {
        data = { ...this.planeList[index] }
        this.planeList.splice(index, 1)
      }
      let index1 = this.aircraftList.findIndex(item => item.id === droneUK);
      this.alarmEvents({ ...data, landFlag: 2 });
      if (index1 > -1) this.aircraftList.splice(index1, 1)
      if (this.isShowPlane) return data;
    },

    // 添加民航飞行器
    addMHPlane(info) {
      // console.log("addMHPlane", info);
      let icon = "minghang.png";
      const { droneUK } = info;
      let index = this.minhangList.findIndex(item => item.id === droneUK);
      let data = {
        icon,
        EntityType: "plane",
        id: droneUK,
        longitude: info.curLongitude,
        latitude: info.curLatitude,
        height: info.curHeight,
        ...info
      }
      if (index > -1) {
        this.minhangList.splice(index, 1, { ...data })
        let index1 = this.aircraftList.findIndex(item => item.id === droneUK);
        this.aircraftList.splice(index1, 1, { ...data })
      } else {
        this.minhangList.push({ ...data })
        this.aircraftList.push({ ...data })
      }
      if (this.isShowMHPlane) return { data: { ...data } };
      return {};
    },
    // 移除民航飞行器
    removeMHPlane(info) {
      const { droneUK } = info;
      let data = null;
      let index = this.minhangList.findIndex(item => item.id === droneUK)
      if (index > -1) {
        // console.log("removeMHPlane>>>>>.>>>>>>", index, droneUK);
        data = { ...this.minhangList[index] }
        this.minhangList.splice(index, 1)
      }
      let index1 = this.aircraftList.findIndex(item => item.id === droneUK);
      if (index1 > -1) this.aircraftList.splice(index1, 1);
      if (this.isShowMHPlane) return data;
    },

    // 控制无人机飞行器显示与隐藏
    controlShowDrone(type) {
      if (type === 1) {
        this.isShowBrandUAV = !this.isShowBrandUAV
        return this.brandUAVList
      } else if (type === 2) {
        this.isShowHomemadeUAV = !this.isShowHomemadeUAV
        return this.homemadeUAVList
      }
    },

    // 控制通航飞行器显示与隐藏
    controlShowPlane() {
      this.isShowPlane = !this.isShowPlane
      return this.planeList
    },

    // 控制民航飞行器显示与隐藏
    controlShowMHPlane() {
      // console.log(this.minhangList);
      this.isShowMHPlane = !this.isShowMHPlane
      return this.minhangList
    },

    // 获取飞行器飞控序列号
    getAircraftSn(id) {
      // console.log(id);
      const data = this.aircraftList.find(item => item.id === id);
      // console.log("mqttInfo", data.droneSerialId);
      return { aircraftId: data.droneSerialId, droneUK: id }
    },
    // 通过 id 获取飞行器详情
    aircraftDetail(droneSerialId) {
      const data = this.aircraftList.find(item => item.droneSerialId === droneSerialId);
      return { ...data }
    },
    // 通过 DroneUk 获取飞行器详情
    getDetailByDroneUk(id) {
      const data = this.aircraftList.find(item => item.id === id);
      return { ...data }
    },

    // 高度搜索
    searchHeight(low, top) {
      if (!low && !top) this.searchList = this.aircraftList;
      else this.searchList = this.aircraftList.filter(item => {
        if (!low && !!top) {
          if (item.curHeight <= top) return item;
        }
        if (!!low && !top) {
          if (item.curHeight >= low) return item;
        }
        if (!!low && !!top) {
          if (item.curHeight >= low && item.curHeight <= top) return item;
        }
      })
      return { removeList: this.aircraftList, showList: this.searchList }
    },

    // 报警
    async alarmEvents(info) {
      // this.alarmList = []
      // console.log(info.intrusionStatus === 1 && info.reportStatus === 0);
      const index = this.alarmList.findIndex(item => item.droneUK === info.droneUK);
      if (index > -1) {
        // 已降落，解除告警
        if (info.landFlag === 2) { this.alarmList.splice(index, 1); return }
        // 报警状态为0，解除告警
        if (info.intrusionStatus === 0 && info.invasionStatus === 0 && info.collisionStatus === 0) {
          this.alarmList.splice(index, 1); return
        }
      }
      if (this.alarmType.alarmHeiFei && info.intrusionStatus === 1 && info.reportStatus === 0) {
        if (index > -1) return;
        if (this.alarmList.length > 9) this.alarmList.pop();
        this.alarmList.unshift({ ...info, alartType: "黑飞报警" })
      } else if (this.alarmType.alarmQinRu && info.intrusionStatus === 1 && info.reportStatus === 1) {
        if (index > -1) return;
        if (this.alarmList.length > 9) this.alarmList.pop()
        this.alarmList.unshift({ ...info, alartType: "侵入报警" })
      } else if (this.alarmType.alarmQinChu && info.invasionStatus === 1) {
        if (index > -1) return;
        if (this.alarmList.length > 9) this.alarmList.pop()
        this.alarmList.unshift({ ...info, alartType: "侵出报警" })
      } else if (this.alarmType.alarmChongTu && info.collisionStatus === 1) {
        if (index > -1) return;
        if (this.alarmList.length > 9) this.alarmList.pop()
        this.alarmList.unshift({ ...info, alartType: "冲突报警" })
      }
      // else if (alarmYiJian && info.intrusionStatus === 1) {
      //   if (index > -1) return;
      //   if (this.alarmList.length > 9) this.alarmList.pop()
      //   this.alarmList.unshift({ ...info, alartType: "一键报警" })
      // };


      // this.aircraftList.forEach(item => {
      //   console.log("this.alarmList.length > 9", this.alarmList.length > 9);

      //   if (item.type === 7) return;
      //   if (this.alarmType.alarmHeiFei && item.intrusionStatus === 1 && item.reportStatus === 0) {

      //     if (this.alarmList.length > 9) this.alarmList.pop()
      //     this.alarmList.unshift({ ...item, alartType: "黑飞报警" })
      //   } else if (this.alarmType.alarmQinRu && item.intrusionStatus === 1 && item.reportStatus === 1) {
      //     if (this.alarmList.length > 9) this.alarmList.pop()
      //     this.alarmList.unshift({ ...item, alartType: "侵入报警" })
      //   } else if (this.alarmType.alarmQinChu && item.invasionStatus === 1) {
      //     if (this.alarmList.length > 9) this.alarmList.pop()
      //     this.alarmList.unshift({ ...item, alartType: "侵出报警" })
      //   } else if (this.alarmType.alarmChongTu && item.collisionStatus === 1) {
      //     if (this.alarmList.length > 9) this.alarmList.pop()
      //     this.alarmList.unshift({ ...item, alartType: "冲突报警" })
      //   }
      //   // else if (alarmYiJian && item.intrusionStatus === 1) {
      //   //   if (this.alarmList.length > 9) this.alarmList.pop()
      //   //   this.alarmList.unshift({ ...item, alartType: "一键报警" })
      //   // };
      // })
    },

    // 报警类型
    async getAlarmType() {
      const { rows } = await getList({ status: 0 });
      let alarmHeiFei = rows.some(item => item.alartType === 0)
      let alarmQinRu = rows.some(item => item.alartType === 1)
      let alarmQinChu = rows.some(item => item.alartType === 2)
      let alarmChongTu = rows.some(item => item.alartType === 3)
      let alarmYiJian = rows.some(item => item.alartType === 4)
      this.alarmType = { alarmHeiFei, alarmQinChu, alarmQinRu, alarmChongTu, alarmYiJian }
    },

    // 清空报警list
    clearMqttWaring() {
      this.tempList = this.alarmList;
      this.alarmList = [];
    },

    // 恢复报警list
    rebackMqttWaring() {
      this.alarmList = this.tempList;
      this.tempList = [];
    }
  }
})

export default useMqttStore;